package com.xingshentech.ugv.operate.module.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xingshentech.ugv.operate.R;
import com.xingshentech.ugv.operate.base.CommonRecyclerAdapter;
import com.xingshentech.ugv.operate.module.bean2.CarListBean;
import com.xingshentech.ugv.operate.module.bean2.DeviceStatusEnum;
import com.xingshentech.ugv.operate.module.listener.OnMultiClickListener;
import com.xingshentech.ugv.operate.module.login.CarListActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CarListAdapter extends CommonRecyclerAdapter<CarListBean> {
    public static String carBean = "carBean";
    private Context mContext;
    private List<CarListBean> mBeanList;


    public CarListAdapter(Context mContext, List<CarListBean> beanList) {
        super(R.layout.item_carlist, beanList);
        this.mContext = mContext;
        this.mBeanList = beanList;
    }


    OnItemChildClickListener mOnItemChildClickListener;

    @Override
    public void convert(@NotNull BaseViewHolder holder, CarListBean dataBean) {
        TextView tvNameCar = holder.getView(R.id.tv_name_car);
        TextView tvCarState = holder.getView(R.id.tv_carState);
        ConstraintLayout parent = holder.getView(R.id.parent);
        TextView tv_subscribe = holder.getView(R.id.tv_subscribe);
        TextView tv_device_control = holder.getView(R.id.tv_device_control);
        TextView tv_stop_charge = holder.getView(R.id.tv_stop_charge);
        TextView tv_deviceState = holder.getView(R.id.tv_deviceState);

        CarListBean.VehicleBean vehicle = dataBean.getVehicle();
        String verificationCode = "";
        if (vehicle != null) {
            verificationCode = vehicle.getVerificationCode() != null ? vehicle.getVerificationCode() : "";
        }
        if (mContext instanceof CarListActivity) {
            CarListActivity.strV(verificationCode);
        }

        if (vehicle != null) {
            tvNameCar.setText(vehicle.getVehicleName());
            String vehicleStatus = vehicle.getVehicleStatus();
            if (vehicle.isOnline()) {
                tvCarState.setText("在线");
                tvCarState.setTextColor(mContext.getResources().getColor(R.color.green));
            } else {
                tvCarState.setText("离线");
                tvCarState.setTextColor(mContext.getResources().getColor(R.color.gray_9));
            }
        }
        if (vehicle.getDeviceStatus() == 10 || vehicle.getDeviceStatus() == 13) {
            tv_device_control.setVisibility(View.VISIBLE);
        } else {
            tv_device_control.setVisibility(View.GONE);
        }
        tv_deviceState.setText("设备状态:" + DeviceStatusEnum.match(vehicle.getDeviceStatus()).getMsg() + "");
        parent.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChild(v, dataBean);
                }
            }
        });
        tv_subscribe.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChild(v, dataBean);
                }
            }
        });
        tv_device_control.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChild(v, dataBean);
                }
            }
        });
        tv_stop_charge.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChild(v, dataBean);
                }
            }
        });
    }

    public interface OnItemChildClickListener {
        void onItemChild(View view, CarListBean carListBean);
    }

    public void setOnItemChildClick(OnItemChildClickListener mOnItemChildClickListener) {
        this.mOnItemChildClickListener = mOnItemChildClickListener;
    }


}
